/**
 * Implementation of file data loader
 *
 * author: Marcin Wachulski
 *
 */
#include "DataSetLoader.h"

DataStructure* loadDataSet(const char* filepath) {
    if (filepath == NULL || filepath == "")
        throw std::exception("Unspecified file path");
    std::ifstream input;
    input.open(filepath, std::ifstream::in);
    if (!input.good())
        throw std::exception("Input file corrupted");

    DataStructure *result = new DataStructure();
    char nextChar = input.peek();
    int currAttr = 0, currObj = 1;
    AttributeSet *currAttrSet = new AttributeSet();
    
    while (input.good()) {
        input >> currAttr;
        currAttrSet->data.insert(currAttr);
        result->updateAttribute(currAttr, currObj);
        // new Object (Transaction or Line) recognized
        nextChar = input.peek();
        while (nextChar == ' ' || nextChar == '\t') {
            input.get();
            nextChar = input.peek();
        }
        if (nextChar == '\n' || input.eof()) {
            result->addObject(currObj, currAttrSet);
            currObj++;
            currAttrSet = new AttributeSet();
            nextChar = input.peek();
            while (input.good() && 
                nextChar == ' ' || nextChar == '\t' || nextChar == '\n') {
                    input.get();
                    nextChar = input.peek();
            }
        }
    }
    delete currAttrSet;

    return result;
}